<?php
/**
 * User: Hufeng
 * Date: 2017/12/04 17:51
 * Desc: 定时器
 */
namespace App\Controller;

use App\Common\Util\Redis;
use App\Model\TradeDelegateHistoryModel;
use App\Model\TradeUserAssetModel;
use App\Model\UserAssetsModel;

class Timer {

    //撤销委托
    public function revokeDelegate(){
        $dateNow = date('Y-m-d');
        $redis = new Redis();
        $tdgModel = new TradeDelegateHistoryModel();
        $tdgList = $tdgModel->queryData(['addtime[<]'=>$dateNow,'left_num[>]'=>0]);
        foreach ($tdgList as $tdgRes){
            $sid = $tdgRes['sid'];
            $price = round($tdgRes['delegate_price'],2);
            $uid = $tdgRes['uid'];
            $id = $tdgRes['id'];
            if($tdgRes['type'] == 1){//买
                $redis->getLockEnabel(USER_STOCK_SALE_RKEY.$sid);
                $priceJson = $redis->client->get(USER_STOCK_BUY_PRIICE.$sid.'_'.$price);
                $myDelegate=$otherDelegate=[];
                if($priceJson){
                    $priceArr = json_decode($priceJson,true);
                    foreach ($priceArr as $darr){
                        if($darr['did'] == $id){
                            $myDelegate = $darr;
                        }else{
                            $otherDelegate[] = $darr;
                        }
                    }
                }
                $leftNum = $myDelegate['dnum'];
                $buyAvgPrice = $myDelegate['avgprice'];
                $rbackMoney = round($leftNum*$buyAvgPrice,2);
                //更新表
                if(!$otherDelegate){
                    //删除
                    $delKeya = USER_STOCK_BUY_PRIICE.$sid.'_'.$price;
                    $delKeyb = USER_STOCK_BUY.$sid;
                    $redis->client->del([$delKeya]);
                    $redis->client->zrem($delKeyb,$price);
                }else{
                    $redis->client->set(USER_STOCK_BUY_PRIICE.$sid.'_'.$price,json_encode($otherDelegate));
                }
                $tdgModel->editData(['id'=>$id],['left_num'=>0]);
                //退还用户资金
                $uaModel = new UserAssetsModel();
                $uaModel->addAssets($uid,$rbackMoney,BALANCE_MONEY,BALANCE_ENABLE,'自动撤销买入');
                $redis->removeLock(USER_STOCK_SALE_RKEY.$sid);
            }elseif($tdgRes['type'] == 2){//卖
                $redis->getLockEnabel(USER_STOCK_BUY_RKEY.$sid);
                $priceJson = $redis->client->get(USER_STOCK_SALE_PRIICE.$sid.'_'.$price);
                $myDelegate=$otherDelegate=[];
                if($priceJson){
                    $priceArr = json_decode($priceJson,true);
                    foreach ($priceArr as $darr){
                        if($darr['did'] == $id){
                            $myDelegate = $darr;
                        }else{
                            $otherDelegate[] = $darr;
                        }
                    }
                }
                $rbackNum = $myDelegate['dnum'];
                //更新表
                if(!$otherDelegate){
                    //删除
                    $delKeya = USER_STOCK_SALE_PRIICE.$sid.'_'.$price;
                    $delKeyb = USER_STOCK_SALE.$sid;
                    $redis->client->del([$delKeya]);
                    $redis->client->zrem($delKeyb,$price);
                }else{
                    $redis->client->set(USER_STOCK_SALE_PRIICE.$sid.'_'.$price,json_encode($otherDelegate));
                }
                $tdgModel->editData(['id'=>$id],['left_num'=>0]);
                //退还用户股票
                $tuaModel = new TradeUserAssetModel();
                $tuaModel->editUserHold($uid,$sid,$rbackNum,$price,'自动撤销卖出');
                $redis->removeLock(USER_STOCK_BUY_RKEY.$sid);
            }
        }
    }
}
